package com.sinosoft.dsp.persistence.businessconcept.utils;

import java.math.BigDecimal;

import com.sinosoft.builder.utils.EntityAttributeType;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.utils.DateUtils;

/**
 * 业务概念帮助类
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-5-20 下午12:05:22
 * 
 */
public class BusinessConceptContentHelper {

	private static final Log log = Log
			.getLog(BusinessConceptContentHelper.class.getName());

	public static Class<?> getAttributeValueType(EntityAttributeType type) {
		if (type == null) {
			type = EntityAttributeType.TYPE_NONE;
		}
		if (type == EntityAttributeType.TYPE_NONE)
			return String.class;
		else if (type == EntityAttributeType.TYPE_INTEGER)
			return Long.class;
		else if (type == EntityAttributeType.TYPE_NUMBER)
			return Double.class;
		else if (type == EntityAttributeType.TYPE_BIGNUMBER)
			return java.math.BigDecimal.class;
		else if (type == EntityAttributeType.TYPE_STRING)
			return String.class;
		else if (type == EntityAttributeType.TYPE_CHAR)
			return Character.class;
		else if (type == EntityAttributeType.TYPE_DATE)
			return Long.class;
		else if (type == EntityAttributeType.TYPE_TIME)
			return Long.class;
		else if (type == EntityAttributeType.TYPE_DATETIME)
			return Long.class;
		else if (type == EntityAttributeType.TYPE_DURATION)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoDurationBean.class;
		else if (type == EntityAttributeType.TYPE_BLOB)
			return Byte[].class;
		else if (type == EntityAttributeType.TYPE_CLOB)
			return Character[].class;
		else if (type == EntityAttributeType.TYPE_TEXT)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoTextBean.class;
		else if (type == EntityAttributeType.TYPE_IMAGE)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoImageBean.class;
		else if (type == EntityAttributeType.TYPE_RANGE)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoRangeBean.class;
		else if (type == EntityAttributeType.TYPE_QUANTITY)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoQuantityBean.class;
		else if (type == EntityAttributeType.TYPE_UNIT)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoUnitBean.class;
		else if (type == EntityAttributeType.TYPE_LOCATION)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoLocationBean.class;
		else if (type == EntityAttributeType.TYPE_DIRECTION)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoDirectionBean.class;
		else if (type == EntityAttributeType.COMPLEX_TYPE)
			return com.sinosoft.dsp.persistence.businessconcept.atom.SinoComplexTypeBean.class;
		return String.class;
	}

	/**
	 * 通过属性类型获得属性值对象
	 * 
	 * @param <T>
	 * @param type
	 * @param parameter
	 * @return
	 * @throws SinoException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T createNewAttribute(EntityAttributeType type,
			String... parameter) throws SinoException {
		try {
			if (type == EntityAttributeType.TYPE_NONE) {
				if (parameter != null && parameter.length > 0) {
					return (T) parameter[0];
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_INTEGER) {
				if (parameter != null && parameter.length > 0) {
					return (T) Integer.valueOf(parameter[0]);
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_NUMBER) {
				if (parameter != null && parameter.length > 0) {
					return (T) Double.valueOf(parameter[0]);
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_BIGNUMBER) {
				if (parameter != null && parameter.length > 0) {
					return (T) new BigDecimal(parameter[0]);
				}
				return (T) BigDecimal.ZERO;
			} else if (type == EntityAttributeType.TYPE_STRING) {
				if (parameter != null && parameter.length > 0) {
					return (T) parameter[0];
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_CHAR) {
				if (parameter != null && parameter.length > 0) {
					return (T) Character.valueOf(parameter[0].charAt(0));
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_DATE
					|| type == EntityAttributeType.TYPE_TIME
					|| type == EntityAttributeType.TYPE_DATETIME) {
				if (parameter != null && parameter.length > 0) {
					if (parameter.length == 1)
						return (T) DateUtils.str2dat(parameter[0]);
					else {
						return (T) DateUtils
								.str2dat(parameter[0], parameter[1]);
					}
				}
				return null;
			} else if (type == EntityAttributeType.TYPE_BLOB) {
				int idx = Integer.valueOf(parameter[0]);
				return (T) (new Byte[idx]);
			} else if (type == EntityAttributeType.TYPE_CLOB) {
				int idx = Integer.valueOf(parameter[0]);
				return (T) (new Character[idx]);
			} else if (type == EntityAttributeType.TYPE_DURATION) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoDurationBean();
			} else if (type == EntityAttributeType.TYPE_TEXT) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoTextBean();
			} else if (type == EntityAttributeType.TYPE_IMAGE) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoImageBean();
			} else if (type == EntityAttributeType.TYPE_RANGE) {
				if (parameter != null && parameter.length > 0)
					if (EntityAttributeType.TYPE_INTEGER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoRangeBean<Long>();
					} else if (EntityAttributeType.TYPE_NUMBER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoRangeBean<Double>();
					} else if (EntityAttributeType.TYPE_BIGNUMBER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoRangeBean<java.math.BigDecimal>();
					}
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoRangeBean();
			} else if (type == EntityAttributeType.TYPE_QUANTITY) {
				if (parameter != null && parameter.length > 0)
					if (EntityAttributeType.TYPE_INTEGER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoQuantityBean<Long>();
					} else if (EntityAttributeType.TYPE_NUMBER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoQuantityBean<Double>();
					} else if (EntityAttributeType.TYPE_BIGNUMBER.getCode()
							.equalsIgnoreCase(parameter[0])) {
						return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoQuantityBean<java.math.BigDecimal>();
					}
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoQuantityBean();
			} else if (type == EntityAttributeType.TYPE_UNIT) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoUnitBean();
			} else if (type == EntityAttributeType.TYPE_LOCATION) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoLocationBean();
			} else if (type == EntityAttributeType.TYPE_DIRECTION) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoDirectionBean();
			} else if (type == EntityAttributeType.COMPLEX_TYPE) {
				return (T) new com.sinosoft.dsp.persistence.businessconcept.atom.SinoComplexTypeBean();
			}
			return null;
		} catch (SinoException e) {
			throw e;
		} catch (Exception e) {
			log.error("创建属性出现错误：" + e.getMessage());
			throw new SinoException("创建属性出现错误。");
		}
	}
}
